//
//  PhoneNumberViewController.m
//  DianDangDai
//
//  Created by bhf on 2017/12/20.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "PhoneNumberViewController.h"
#import "UIButton+WebCache.h"
@interface PhoneNumberViewController ()
{
    NSString *_imgUUID;
    NSString *_imgURL;
    NSString *_phone;
    NSString *_pwd;
    NSString *_referrer;
    NSString *_msg;
}
@end

@implementation PhoneNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"安全设置";
    self.bottomBtn.layer.cornerRadius = 22;
    [self getImage];
}
- (void)getImage {
    NSString *urlString = [NSString stringWithFormat:@"%@OPT=1011",API_LOGIN_URL_STRING];
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        NSLog(@"%@",dict);
        _imgUUID = dict[@"randomId"];
        _imgURL = dict[@"img"];
        [_imgBtn sd_setImageWithURL:[NSURL URLWithString:_imgURL] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
    } failure:^(NSError *error) {
        
    } showHUD:YES];
}
- (IBAction)refreshImg:(UIButton *)sender {
    [sender sd_setImageWithURL:[NSURL URLWithString:_imgURL] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
}
- (IBAction)getMsgCode:(UIButton *)sender {
    
    __block int timeout= 119; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                [sender setTitle:@"获取验证码" forState:0];
                [sender setTitleColor:[UIColor colorWithHex:@"#FDDC00"] forState:UIControlStateNormal];
                sender.userInteractionEnabled = YES;
            });
            
        }else{
            
            //            int minutes = timeout / 60;
            
            int seconds = timeout % 120;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [sender setTitle:[NSString stringWithFormat:@"%@S后获取",strTime] forState:UIControlStateNormal];
                
                sender.userInteractionEnabled = NO;
                
            });
            
            timeout--;
            
        }
        
    });
    
    dispatch_resume(_timer);
    
    NSString *urlString = [NSString stringWithFormat: @"%@OPT=1037&mobile=%@&cCode=%@&randomId=%@",API_LOGIN_URL_STRING,_phoneNumber.text,_imgCode.text,_imgUUID];
    
    NSLog(@"%@",urlString);
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        NSLog(@"%@",dict);
        [self.view makeToast:dict[@"msg"]];
    } failure:^(NSError *error) {
        
    } showHUD:YES];
}
    
- (IBAction)commit:(UIButton *)sender {
    NSString *urlString = [NSString stringWithFormat: @"%@OPT=1043&mobile=%@&cCode=%@&sign=%@",API_LOGIN_URL_STRING,_phoneNumber.text,_msgCode.text,USER_SIGN];
    
    NSLog(@"%@",urlString);
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        NSLog(@"%@",dict);
        [self.view makeToast:dict[@"msg"]];
    } failure:^(NSError *error) {
        
    } showHUD:YES];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
